<?php

class BoardController extends MemberController {

    public function actionReport() {

        $board_network = vo_network::get_board_network(Yii::app()->user->getState('network_id'), '0');
        $akumulasi_sponsor = vo_network::get_accumulative_sponsor_board(Yii::app()->user->getState('network_id'), '1');
        
        $this->render('report', array(
            'board_network' => $board_network,
            'akumulasi_sponsor' => $akumulasi_sponsor,
        ));
    }

    public function actionRoll() {

        $board_network = vo_network::get_board_network(Yii::app()->user->getState('network_id'), '1');

        $this->render('roll', array(
            'board_network' => $board_network,
        ));
    }

    public function actionGeneology() {

        $is_cycle = (function_lib::uri_segment(4)== 'cycle' ) ? "1" : "0";
        $board_leader_board_network_mid = function_lib::uri_segment(5);
        $board_class = function_lib::uri_segment(6);
        if ($board_leader_board_network_mid != '') {
            $board_leader_board_network_id = vo_network::get_board_network_id($board_leader_board_network_mid);
            if ($board_leader_board_network_id != false) {
                //$board_network = vo_network::get_board_network(Yii::app()->user->getState('network_id'),$is_cycle,$board_class);
                $board_network = vo_network::get_data_board_network_id($board_leader_board_network_id,$is_cycle,$board_class);
                $check_network_group = false;
                if($board_network != false) {
                    foreach ($board_network as $id => $row_network) {
                        $check_network_group = vo_network::check_network_group($board_leader_board_network_id, $row_network['board_network_id'],$is_cycle);
//                        if ($check_network_group == false) {
//                            echo "leader $board_leader_board_network_id ".$row_network['board_network_id'].""
//                            ;
//                        }
//                        echo "<pre>";
//                        echo $check_network_group;
//                        print_r($board_network);
//                        die();
                    }
                }
                
                if ($check_network_group != false) {
                    $data = function_lib::generate_geneology_board_binary($board_leader_board_network_id,$is_cycle);
                    
                    $follower = '';
                    if($board_class == '2' ){
                        $follower = $this->get_follower($board_leader_board_network_mid);
                    }

                    $this->render('geneology', array(
                        'data' => $data,
                        'follower' => $follower,
                    ));
                } else {
                    Yii::app()->controller->redirect(array('/member/board/report/a'));
                }
            } else {
                Yii::app()->controller->redirect(array('/member/board/report/b'));
            }
        } else {
            Yii::app()->controller->redirect(array('/member/board/report/c'));
        }
    }

    public function get_follower($network_id){

        $network_board_mid = function_lib::uri_segment(5);
        $board_network_id = function_lib::get_one("board_network_id", "sys_board_network", "board_network_network_mid='$network_board_mid'");

        $sql = "Select * from sys_board_follower where board_follower_board_sponsor_network_id = '$board_network_id'";
        $res = Yii::app()->db->createCommand( $sql )->queryAll();

//        foreach($res as $row ){
//            $board_network_mid = function_lib::get_one("board_network_network_mid", "sys_board_network", "board_network_id='".$row['board_follower_board_network_id']."'");
//            //disinilah diecho mid dikotak2in
//        }
        return $res;

    }

}
